home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Collections: MegaDisc
/
MegaDisc 43 (1995-03)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2)[m bamcopy].zip
/
MegaDisc 43 (1995-03)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2)[m bamcopy].adf
/
Programming
/
Pascal_Tutes
/
Tute11.pas
< prev
next >
Wrap
Pascal/Delphi Source File
|
1995-01-27
|
7KB
|
202 lines
{
«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»
«» «»
«» TUTORIAL ELEVEN «»
«» «»
«»
by
«»
«» «»
«»
Anthony Peck
«»
«» «»
«» «»
«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»
43 !¡ 43 !¡ 43 !¡ 43 !¡ 43 !¡ 43 !¡ 43 !¡ 43
It's just a small procedure, it won't hurt a bit...
Some of the programs in the last set of tutorials were starting to get
too big for their own boots! Take Tute8.pas for instance, which had
several variables, and a couple of different components within the
program structure.
A Program is more manageable both to code and debug if it is broken up
into smaller tasks. This is called modularity, and the theory is that
each component of the program be no more than a page or so in length.
Pascal allows modularity through
PROCEDURES
, which are like separate
little programs within the main program.
Let's make a program that finds the cube of a number entered by the
user. For any number x the cube would be x³. 4³ is 4x4x4 = 64! It
would be nice just to be able to separate out the parts of the program
to do different jobs. For example we could have three procedures which
do the following tasks...
Get number;
Find cube;
Display result;
Isn't this exciting! On with the program...
}
Program MDTute11 (Input,Output);
{ This program finds the cube of a user entered number
Author : A N Peck
Date : 23 October 1994
Procedures used:
Getnumber - prompts user for a number
Cube - multiplies a number by itself three times
Display - displays the result }
{
^
|
|__
This information is handy to have at the start of a
program which is going to use procedures. It just
keeps the code tidy, and makes lovely dressing for a
side salad.
}
Var number,result: longint;
{ ---------------------------------------------------------- }
{
^
|
|__
I use one of these to separate out the procedures. You don't
need them at all, but it makes the code easier to read and
follow. It's another one of those damn cosmetic artifacts.
}
Procedure Getnumber;
{
^
|
|__
Each procedure commences with the word Procedure, followed by the
name which you have decided best fits the description of what
the procedure is about. You could call them Ralph, Graham and
Mindy but that doesn't tell the average punter too much about
what is really happening!
}
{ Prompts user for a number }
{
^
|
|__
It's a good policy to start each procedure with a comment
describing what the procedure achieves.
}
begin
{
^
|
|__
The procedure begins with a "begin"!
}
writeln;
write(' Please enter a whole number: ');
readln(number);
end; { Getnumber }
{
^
|
|__
It's good form to finish with a comment noting the name
of the procedure. You can also insert a small jam tart
here if you're really brave.
}
{ ---------------------------------------------------------- }
Procedure cube;
{ Multiplies a number by itself three times }
begin
result := number*number*number;
{
^
|
|__
This procedure contains just the maths bit.
}
end; { cube }
{ ---------------------------------------------------------- }
Procedure display;
{ Displays the results }
begin
writeln;
writeln(' ','The cube of number ',number,' is ',result);
writeln;
end; { display }
{ ---------------------------------------------------------- }
{ ---------------------------------------------------------- }
begin { Main Program }
Getnumber;
Cube;
Display;
{
^
|
|__
The whole of the main program boils down to just three lines.
The hard work has been farmed out to the procedures, which
makes the main program look rather wonderful. Is that easy
or am I just a silly man in a big hat?
}
end. { Main Program }
{ ---------------------------------------------------------- }
{
Well! All that for just $1.99 plus tax. We didn't really need
three procedures for such a simple task, but it does make the
project a bit easier to follow. Now if we see a bug, we can
find in which part of the program it might be located a bit faster.
Piecemeal examination of a large program is much easier than trying
to visualise the entire source code in one hit.
If you're bored to death tonight, try to break up tute8.pas into
procedures. Use the pseudocode first, and then onwards to the
completed task!
«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»
«» «»
«»
Non compos mentis
«»
«» «»
«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»
43 !¡ 43 !¡ 43 !¡ 43 !¡ 43 !¡ 43 !¡ 43 !¡ 43
}